package com.laowang.project.websocket;


import com.laowang.framework.web.domain.AjaxResult;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

/**
 * WebSocketController
 *
 * @author zhengkai.blog.csdn.net
 */
@Controller
public class DemoController {

    @GetMapping("index")
    @ResponseBody
    public ResponseEntity<String> index() {
        return ResponseEntity.ok("请求成功");
    }


    // http://localhost:9000/page
    @GetMapping("/page")
    public String page() {
        return "websocket";
    }


    //http://localhost:8080/push/10?message=123123
    @PostMapping("/push/{toUserId}")
    @ResponseBody
    public AjaxResult pushToWeb(String message, @PathVariable("toUserId") String toUserId) throws IOException {
        TbMessage messageObj = new TbMessage();
        messageObj.setContent(message);
        messageObj.setType(1);
        messageObj.setCreateTime(new Date());
        WebSocketServer.sendInfo(messageObj, toUserId);

        return AjaxResult.success("成功");
    }

    //http://localhost:8080/push?msg=7899   或者  实现服务器的主动推送
    @RequestMapping("/push")
    @ResponseBody
    public AjaxResult pushToWeb1() throws IOException {
        TbMessage tbMessage = new TbMessage();
        tbMessage.setContent("测试");
        tbMessage.setCreateTime(new Date());
        tbMessage.setType(1);
        WebSocketServer.sendAll(tbMessage);
        return AjaxResult.success();
    }
}

